<?php
//
// Classes.class.php
//

// Direct access protection
if( !defined( 'LIRIOPE' )) die( 'Direct access is not allowed.' );

class Classes extends obj {
  // loaded flag holds if the classes object has been initialized already
  var $loaded = FALSE;

  public function __construct( $file ) {
    $found = load::exists($file, c::get('root.web'));
    if( !$found ) trigger_error("We can't find that classes data file", E_USER_ERROR);
    $this->load($found);
  }

  public function __toString() {
    $return = json_encode($this->_);
    return $return . "\n";
  }

  public function load($file) {
    if($this->loaded) return TRUE;
    $yaml = new Yaml($file);

    foreach($yaml->parse() as $key => $class ) {
      $obj = new Course($class);
      $this->$key = $obj;
    }
    $this->loaded = TRUE;
  }

  public function get() {
    // check if any classes were loaded
    if(!$this->loaded) trigger_error("The Classes model hasn't been loaded yet.", E_USER_ERROR);
    // return the array of classes
    return $this->_;
  }

  public function getByID($id) {
    // check if any classes were loaded
    if(!$this->loaded) trigger_error("The Classes model hasn't been loaded yet.", E_USER_ERROR);
    // if the desired class exists, return it
    if(!empty($this->_[$id])) return $this->_[$id];
    return FALSE;
  }

}

class Course extends obj {
  // the next calendar event
  var $nextEvent = array();
  // whether or not the event is one-day
  var $oneDayFlag = FALSE;

  public function __construct($class) {
    foreach($class as $k => $v) {
      $this->$k = $v;
    }
  }

  public function getName() {
    $name = $this->name;
    return empty($name) ? "Unknown Class" : $name;
  }

  public function getURL() {
    $url = $this->url;
    return empty($url) ? "#" : $url;
  }

  public function getExcerpt() {
    $tease = $this->excerpt;
    return empty($tease) ? "" : $tease;
  }

  public function getImages() {
    $images = $this->image;
    if(!is_array($images)) return (array) $images;
    return $images;
  }

  public function getImageThumb() {
    $images = $this->getImages();
    $thumb = $images['thumb'];
    if(empty($thumb)) return FALse;
    return $thumb;
  }

  public function getEventNotes() {
    $event = $this->getNextEvent();
    $notes = $event['notes'];
    return $notes;
  }

  // determineDateRange()
  // decides if the event is a 1-day or many-day event
  //
  // @param  array $event The event to test
  // @return TRUE or FALSE
  //
  private function determineDateRange(&$event) {
    // events must have a begins
    if(empty($event['begins'])) return FALSE;

    $start = strtotime($event['begins']);
    $end = strtotime($event['ends']);
    $one_day = (24*60*60)-1; // 24 hours * 60 mins * 60 secs
    $now = time();

    // if they are a one day event, then ends may be empty. return the start date
    if(empty($event['ends']) || $event['begins'] == $event['ends']) {
      if(($start + $one_day) < $now) {
        return FALSE;
      }
      $event['one_day'] = TRUE;
      return TRUE;
    }
    // if they are a ranged event, then check for end time
    if(($end + $one_day) < $now) {
      return FALSE;
    }
    $event['one_day'] = FALSE;
    return TRUE;
  }

  // getNextEvent()
  //
  // @return mixed The next event string or array, FALSE if none can be found
  //
  public function getNextEvent() {
    // check to see if the next event has been found
    if(!empty($this->nextEvent)) return $this->nextEvent;

    $events = $this->event;
    if(empty($events)) return FALSE;
    foreach($events as $event) {
      if($this->determineDateRange($event)) {
        $this->nextEvent = $event;
        return $event;
      }
    }
    return FALSE;
  }

  // getNextEventDate()
  // returns a string date for a one day event or an array for a date range
  //
  public function getNextEventDate() {
    // check to see if the next event has been found
    $event = $this->getNextEvent();
    $this->getEventDate($event);
    return $event['date'];
  }

  // getEventDate()
  //
  private function getEventDate(&$event) {
    if($event['one_day']) {
      $event['date'] = $event['begins'];
      return TRUE;
    }
    $event['date'] = array($event['begins'], $event['ends']);;
    return TRUE;
  }

  // getUpcomingEvents()
  //
  public function getUpcomingEvents() {
    $events = $this->event;
    $upcoming = array();
    foreach($events as $event) {
      if($this->determineDateRange($event)) {
        $this->getEventDate($event);
        $upcoming[] = $event;
      }
    }
    return $upcoming;
  }

}
